﻿Public MustInherit Class ItemListPage
    Inherits ItemPage

    Protected MustOverride ReadOnly Property DefaultOrder() As OrderCriterion

    Protected MustOverride ReadOnly Property DefaultFilter() As FilterCriterion

    Protected WithEvents ListGrid As GridView

    Protected WithEvents SearchType As DropDownList

    Protected WithEvents SearchBox As TextBox

    Protected WithEvents SearchButton As Button

    Protected WithEvents ResetButton As Button

    Protected WithEvents AddItemLink As HyperLink

    Protected MustOverride Function CreateDataGateway() As DataGateway

    Protected Property OrderBy() As OrderCriterion
        Get
            If ViewState("OrderBy") Is Nothing Then
                ViewState("OrderBy") = DefaultOrder
            End If
            Return ViewState("OrderBy")
        End Get
        Set(value As OrderCriterion)
            ViewState("OrderBy") = value
        End Set
    End Property

    Protected Property FilterBy() As FilterCriterion
        Get
            If ViewState("FilterBy") Is Nothing Then
                ViewState("FilterBy") = DefaultFilter
            End If
            Return ViewState("FilterBy")
        End Get
        Set(value As FilterCriterion)
            ViewState("FilterBy") = value
        End Set
    End Property

    Protected Overridable Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ResetFilter()
            UpdateDataSource()
            UpdateVisibility()
        End If
    End Sub

    Protected Sub searchBox_TextChanged(sender As Object, e As EventArgs) Handles SearchBox.TextChanged
        UpdateFilter()
        UpdateDataSource()
    End Sub

    Protected Sub searchBtn_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        UpdateFilter()
        UpdateDataSource()
    End Sub

    Protected Sub resetBtn_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
        ResetFilter()
        UpdateDataSource()
    End Sub

    Protected Sub UpdateFilter()
        FilterBy = New FilterCriterion(SearchType.SelectedValue, SearchBox.Text)
    End Sub

    Protected Sub ResetFilter()
        FilterBy = Nothing
        SearchType.SelectedValue = FilterBy.Column
        SearchBox.Text = FilterBy.Value
    End Sub

    Protected Sub ListGrid_Sorting(sender As Object, e As GridViewSortEventArgs) Handles ListGrid.Sorting
        If e.SortExpression = OrderBy.Expression Then
            OrderBy.ToggleDirection()
        Else
            OrderBy = New OrderCriterion(e.SortExpression)
        End If
        UpdateDataSource()
    End Sub

    Protected Sub UpdateDataSource()
        Dim gateway As DataGateway = CreateDataGateway()
        If String.IsNullOrWhiteSpace(FilterBy.Value) Then
            ListGrid.DataSource = gateway.FindAll(OrderBy)
        Else
            ListGrid.DataSource = gateway.FindWhere(FilterBy, OrderBy)
        End If
        ListGrid.DataBind()
    End Sub

    Protected Sub UpdateVisibility()
        If AddItemLink IsNot Nothing Then
            AddItemLink.Visible = InsertionPermitted
        End If
    End Sub
End Class
